import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import getOnePsnData from "../../container/functions/getOnePsnData";
import Pagination from "../../../../../../hrpub/common/components/Pagination";
import {debounce} from "../../../../../../hrpub/common/utils/utils";

class MainTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: ''
        };
        this.changePage = this.changePage.bind(this);
        this.pageSizeChange = this.pageSizeChange.bind(this);
        this.rowDoubleClickHandle = this.rowDoubleClickHandle.bind(this);
        this.setTableHeight = this.setTableHeight.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
        window.addEventListener("resize", debounce(this.setTableHeight));
    }

    setTableHeight() {
        const rightContent = document.querySelector('.right-content');
        if (rightContent) {
            this.setState({
                tableHeight: rightContent.clientHeight - 68
            })
        }
    }

    changePage(pageIndex) {
        const {
            updateState,
            getMainTableData,
            pageInfo
        } = this.props;

        updateState({
            pageInfo: {
                ...pageInfo,
                pageIndex: pageIndex
            }
        }, () => {
            getMainTableData();
        });
    }

    rowDoubleClickHandle(record, index, props, e) {
        getOnePsnData.call(this, this.props.orgVal, record.pk_psnjob.value, (data) => {
            this.props.updateState({
                page: 'detail',
                detailTemplate: data,
                isShowBottomDom: false
            });
        })
    }

    pageSizeChange(pageSize) {
        const {
            updateState,
            getMainTableData,
            pageInfo
        } = this.props;

        updateState({
            pageInfo: {
                ...pageInfo,
                pageSize,
                pageIndex: 1
            }
        }, () => {
            getMainTableData();
        });
    }

    onSelected = (props, moduleId, record, index, isSelected) => {
        const {subsetVisible, cIns} = this.props;
        this.props.table.setClickRowIndex('ncc60071702list', {
            record: record,
            index: index
        });
        this.props.updateState({
            focusedRow: index
        });
        setTimeout(() => {
            subsetVisible && cIns.subSetCard.initSubSet();
        }, 1);
    };

    rowClickHandle = (props, moduleId, record, index, e) => {
        const {subsetVisible, cIns} = this.props;

        this.props.updateState({
            focusedRow: index
        });
        setTimeout(() => {
            subsetVisible && cIns.subSetCard.initSubSet();
        }, 1);
    };

    render() {
        const {table, pageInfo, pageHeight, subsetVisible, focusedRow, cIns, subHeight} = this.props;
        let subH = subHeight ? subHeight : pageHeight * 0.3
        return (
            <div className='main-table'>
                {table.createSimpleTable('ncc60071702list', {
                    height: !subsetVisible ? pageHeight - 138 : pageHeight - subH - 138,
                    showCheck: true,
                    showIndex: true,
                    onRowDoubleClick: this.rowDoubleClickHandle,
                    onRowClick: this.rowClickHandle,
                    onSelected: this.onSelected,
                    rowClassList: [{
                        index: focusedRow,
                        className: 'table-focused-row'
                    }]
                })}
                <div
                    style={{
                        paddingTop: '14px'
                    }}
                >
                    {
                        pageInfo.total > 0 &&
                        <Pagination
                            current={parseInt(pageInfo.pageIndex)}
                            pageSize={parseInt(pageInfo.pageSize)}
                            pageSizeOptions={[10, 20, 50, 100, 200]}
                            showSizeChanger={true}
                            total={pageInfo.total}
                            onShowSizeChange={this.pageSizeChange}
                            onChange={this.changePage}
                            showSizeInput={true}
                        />
                    }
                </div>
                {/*<Pagination
                    {...this.props}
                    pageInfo={pageInfo}
                    onPageChange={this.changePage}
                    onPageSizeChange={this.pageSizeChange}/>*/}
            </div>
        );
    }
}

export default MainTable;
